#include "nethandler.h"
#include "client.h"
#include "globalvar.h"
#include "netpack.h"
#include "server.h"

void ClientMsgHandler::OnOpen(CrossSock s)
{
  Client* client=GClientMgr->CreateClient(s);
  if(!GClientMgr->Add(s,client))
  {
    assert(0);
    delete client;
  }
}

void ClientMsgHandler::OnClosed(CrossSock s)
{
  Client* client=GClientMgr->Remove(s);
  assert(client);
  if(client)
  {
    delete client;
  }
}

void ClientMsgHandler::OnReceived(CrossSock s,NetMessage *msg)
{
  Client* client=GClientMgr->Find(s);
  if(client)
  {
    if(msg->datalen>=sizeof(uint16))
    {
      NetPacket pack(msg);
      client->OnRecvNetPacket(pack);
      pack.ClearMsg();
    }
  }
}

void ClientMsgHandler::OnIllegal(CrossSock s,void *data,int datalen)
{
    GNet->close(s);
}
void ClientMsgHandler::OnPrevRecv(CrossSock s,void *data,int *datalen)
{
}
bool ClientMsgHandler::OnSend(CrossSock s,NetMessage *&msg,bool sendhead)
{
    return true;
}

Connection *AppCryptNet::find_connection(CrossSock s)
{
    return GClientMgr->Find(s);
}

void ServerMsgHandler::OnOpen(CrossSock s)
{
    GServerMgr->OnOpen(s);
}

void ServerMsgHandler::OnClosed(CrossSock s)
{
    GServerMgr->OnClosed(s);
}

void ServerMsgHandler::OnReceived(CrossSock s,NetMessage *msg)
{
    GServerMgr->OnReceived(s,msg);
}

void ServerMsgHandler::OnIllegal(CrossSock s,void *data,int datalen)
{
    GCNet->close(s);
}
void ServerMsgHandler::OnPrevRecv(CrossSock s,void *data,int *datalen)
{
}
bool ServerMsgHandler::OnSend(CrossSock s,NetMessage *&msg,bool sendhead)
{
    return true;
}
